<template>
  <TableView
    ref="tableView"
    :table-attrs="{
      api: fetchList,
      descs: ['create_time'],
      dicOptions: [
        { prop: 'status', dicType: 'bulletin_status' },
        { prop: 'typeCode', dicType: 'bulletin_type' }
      ]
    }"
    form-path="/admin/bulletin/bulletinForm/{id:-1}"
    :search-model="searchForm"
    :delete-api="batchDelete"
    :toolbar-attrs="{ buttons: ['slot', { del: 'bulletin_bulletin_del', create: 'bulletin_bulletin_add' }] }">
    <template #toolbar-button="{ selectedRows }">
      <ToolbarButton
        privilege="bulletin_bulletin_push"
        type="primary"
        icon="el-icon-s-promotion"
        :disabled="!selectedRows.length"
        size="small"
        @click.stop="pushBulletin(selectedRows)"
        >发布</ToolbarButton
      >
    </template>
    <template #columns>
      <TableColumn label="状态" prop="status" dict-type="bulletin_status" width="60" />
      <TableColumn label="业务类型" prop="typeCode" dict-type="bulletin_type" width="160" />
      <TableColumn label="主题" clickable prop="subject" />
      <TableColumn label="发布时间" clickable prop="pushTime" width="185" />
      <TableColumn label="发布用户" clickable prop="pushUserName" width="185" />
      <TableColumn label="发布部门" clickable prop="pushUserDeptFullname" width="185" />
      <TableColumn label="最后修改时间" prop="updateTime" width="185" />
    </template>
    <template #search-form>
      <FormSelect v-model="searchForm.status" dict-type="purchaseapply_status" label="状态" prop="status" />
      <FormInput v-model="searchForm.typeCode" label="业务类型" prop="typeCode" />
      <FormInput v-model="searchForm.subject" label="申请单号" prop="subject" />
      <FormDatePicker v-model="searchForm.createTime" label="发布时间" :split="false" date-range prop="createTime" />
    </template>
  </TableView>
</template>

<script>
  import { services } from '@/config'
  import { fetchList, batchDelete, pushBulletin } from '@/api/admin/bulletin'

  export default {
    name: 'BulletinView',
    data() {
      return {
        searchForm: {
          type: '',
          purchaseOuname: '',
          caseid: '',
          createTime: [],
          arrivalDatetime: [],
          createBy: '',
          deptFullname: '',
          budgetNumber: '',
          budgetName: '',
          supplierName: '',
          method: '',
          detailed: '',
          status: ''
        }
      }
    },
    methods: {
      fetchList,
      batchDelete,
      //  导出excel
      exportExcel() {
        this.$downBlobFile(`${services.assetService}bulletin/export`, this.searchForm, '通知公告.xlsx')
      },
      async pushBulletin(selectedRows) {
        await this.$confirm('确认发布公告吗?', '提示', { type: 'warning' })
        const res = await pushBulletin(selectedRows.map(item => item.id))
        if (res.data.data === 0) {
          this.$message.success('发布成功')
        } else {
          this.$message.warning('发布失败')
        }
      }
    }
  }
</script>
